package com.lmpop.frame.action;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

import com.lmpop.frame.SysMap;
import com.lmpop.frame.bpm.BpmInter;
import com.lmpop.frame.util.Config;

public class BaseAction extends Action
{
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
  {
    HttpSession sessioncheck = request.getSession();

    if ((sessioncheck.getAttribute("session_user_id") == null) && (!request.getParameter("bpm_id").equals("2626")) && (!request.getParameter("bpm_id").equals("2627")) && (!request.getParameter("bpm_id").equals("2624")) && (!request.getParameter("bpm_id").equals("2103")) && (!request.getParameter("bpm_id").equals("1378")))
    {
      return mapping.findForward("success");
    }
    if ((request.getParameter("bpm_id") != null) && (request.getParameter("bpm_id").equals("")))
    {
      return mapping.findForward("success");
    }
    String forward_name = "success";
    SysMap sysMap = new SysMap();
    try
    {
      request.setCharacterEncoding("UTF-8");
      Config config = new Config();
      config.init("jdbc.properties");
      String init_database = config.getString("init_database");

      if (init_database.equals("1")) {
        forward_name = "init";
      } else {
        XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
        BpmInter bpmMgr = (BpmInter)factory.getBean("trade");
        HttpSession session = request.getSession();
        sysMap.convert(request, session);
        if ((sysMap.getString("result_code") != null) && (!sysMap.getString("result_code").equals("")))
        {
          if (sysMap.getString("result_code").equals("9"))
          {
            request.setAttribute("sysMap", sysMap);
            return mapping.findForward(forward_name);
          }
        }
        if ((sysMap.getString("bpm_id") != null) && (!sysMap.getString("bpm_id").equals("")))
        {
          if ((!request.getParameter("bpm_id").equals("2627")) && (!request.getParameter("bpm_id").equals("2626")) && (!request.getParameter("bpm_id").equals("2624")) && (!request.getParameter("bpm_id").equals("2103")) && (!request.getParameter("bpm_id").equals("1378")))
          {
            if ((sysMap.getString("session_user_id") == null) || (sysMap.getString("session_user_id").equals("")))
            {
              return mapping.findForward(forward_name);
            }
          }
        }
        sysMap = bpmMgr.bpmMgr(sysMap);

        String cookietime = sysMap.getString("cookietime");
        String result_code = sysMap.getString("result_code");
        if ((!cookietime.equals("")) && (result_code.equals("0"))) {
          setCookieBySessionList(response, cookietime, sysMap);
        }

        request.setAttribute("sysMap", sysMap);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }

    return mapping.findForward(forward_name);
  }

  public void setCookieBySessionList(HttpServletResponse response, String cookietime, SysMap sysMap) {
    ArrayList sessionList = sysMap.getSessionList();
    if ((sessionList != null) && (sessionList.size() > 0)) {
      Hashtable tempMap = new Hashtable();
      for (int i = 0; i < sessionList.size(); i++) {
        tempMap = (Hashtable)sessionList.get(i);
        String name = ""; String value = "";
        Iterator iter = tempMap.keySet().iterator();
        while (iter.hasNext()) {
          name = iter.next().toString();
          if (name.equals("session_user_name"))
          {
            try
            {
              value = URLEncoder.encode(tempMap.get(name).toString(), "UTF-8");
            }
            catch (UnsupportedEncodingException e)
            {
              e.printStackTrace();
            }continue;
          }

          value = tempMap.get(name).toString();
        }

        Cookie userCookie = new Cookie(name, value);
        if (isNotNum(cookietime))
          userCookie.setMaxAge(Integer.parseInt(cookietime));
        else {
          userCookie.setMaxAge(2592000);
        }

        response.addCookie(userCookie);
      }
    }
  }

  public boolean isNotNum(String cookietime) {
    Pattern p = Pattern.compile("\\d");
    Matcher m = p.matcher(cookietime);
    return m.matches();
  }
}